<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="24">
        <el-form :inline="true" :model="queryParams" ref="queryForm" size="small" label-width="0"
                 class="demo-form-inline" style="height: 40px;float: right;">
          <el-form-item prop="receiptNo">
            <el-input
              v-model="queryParams.businessNo"
              placeholder="业务单号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item style="width: 100px;" prop="status">
            <el-select v-model="queryParams.status" placeholder="状态" clearable size="small">
              <el-option
                v-for="dict in dict.advance_receipt_status"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker
              v-model="billTime"
              type="daterange"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="预收日期开始"
              end-placeholder="预收日期结束">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"></el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"></el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <!--<el-row :gutter="10" class="mb8">-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="primary"-->
          <!--plain-->
          <!--size="mini"-->
          <!--:disabled="multiple"-->
          <!--@click="handleAdvanceRefund "-->
        <!--&gt;预收退款</el-button>-->
      <!--</el-col>-->
    <!--</el-row>-->
    <el-table v-loading="loading" :data="advanceReceiptList"    @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预收单号" align="center" prop="no"/>
      <el-table-column label="预收来源" align="center" prop="businessType">
        <template slot-scope="scope">
          {{ dict.label.business_type[scope.row.businessType] }}
        </template>
      </el-table-column>
      <el-table-column label="业务单号" align="center" prop="businessNo"/>
      <el-table-column label="预收金额" align="center" prop="billMoney"/>
      <el-table-column label="预收日期" align="center" prop="billTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="剩余金额" align="center" prop="amountRemaining"/>
      <el-table-column label="操作" align="center" width="150" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleRecord(scope.row)"
          >预收款余额使用记录
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <against-record-list ref="recordRef" />
  </div>
</template>

<script>

  import {listMixin} from "@/utils/mixins/listMixin";
  import {listAdvanceReceipt , advanceRefund } from "@/api/payfee/advanceReceipt";
  import AgainstRecordList from "./againstRecordList"

  export default {
    name: "AdvanceReceipt",
    mixins: [listMixin],
    dicts: ['advance_receipt_status', 'business_type'],
    components:{ AgainstRecordList },
    data() {
      return {
        // 预收单表格数据
        advanceReceiptList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          status: null,
          businessNo: null,
          params: {}
        },
        currentOptions: {},
        //预收日期
        billTime: [],
        selection:[]
      };
    },
    methods: {
      init(options) {
        this.currentOptions = options
        this.getList();
      },
      /** 查询预收单列表 */
      getList() {
        this.loading = true;
        this.queryParams.params.roomIds = this.currentOptions.roomIds || "-1"
        let queryParams = this.addDateRange(this.queryParams, this.billTime, "billTime")
        listAdvanceReceipt(queryParams).then(response => {
          this.advanceReceiptList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.billTime = []
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.madvanceReceiptId)
        this.single = selection.length!==1
        this.multiple = !selection.length
        this.selection = selection
      },
      handleRecord(row){
        this.$refs.recordRef.show({madvanceReceiptId:row.madvanceReceiptId})
      },
      handleAdvanceRefund(){
        let ysList = this.selection.filter(row=>row.amountRemaining <= 0)
        if(ysList.length > 0){
          let nos = ysList.map(item => item.no)
          this.msgWarning("预收单["+nos+"]没有剩余余额,不能退款。")
          return false;
        }
        const ids = this.ids ;
        const nos = this.selection.map(item => item.no)
        this.$confirm('是否确认退预收款编号为"' + nos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return advanceRefund(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("退款成功");
        }).catch(() => {
        });
      }
    }
  };
</script>
